package cn.hn.tx.rebot.task.job;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.hn.comm.Tools;
import cn.hn.tx.rebot.blog.model.Blog;
import cn.hn.tx.rebot.blog.model.Comment;
import cn.hn.tx.rebot.blog.service.ICrawlBlogService;
import cn.hn.tx.rebot.blog.service.IRebotBlogService;
import cn.hn.tx.rebot.task.model.TaskExeLog;

import com.eic.core.spring.BeanFactoryUtil;

public class TaskJob_CrawlComment extends ITaskJob {

	private IRebotBlogService rebotBlogService;

	public static void main(String[] args) {
		Comment c1 = new Comment();
		c1.setComment("1");
		Comment c2 = new Comment();
		c2.setComment("2");
		Comment c3 = new Comment();
		c3.setComment("3");
		Comment c4 = new Comment();
		c4.setComment("4");

		Comment cA = new Comment();
		cA.setComment("1");
		Comment cB = new Comment();
		cB.setComment("2");
		Comment cC = new Comment();
		cC.setComment("3");
		Comment cD = new Comment();
		cD.setComment("4");

		List<Comment> listA = new ArrayList<Comment>();
		listA.add(c1);
		listA.add(c2);
		listA.add(c3);
		listA.add(c4);

		List<Comment> listB = new ArrayList<Comment>();
		listB.add(cA);
		listB.add(cB);
		listB.add(cC);

		TaskJob_CrawlComment tc = new TaskJob_CrawlComment();
		List<Comment> listResult = tc.deleteExistComment(listA, listB);
		for (Comment c : listResult) {
			System.out.println(c.getComment());
		}

	}

	@Override
	void executeJob() {
		System.out.println("开始:" + getTaskName());
		// 任务执行记录日志
		TaskExeLog taskExeLog = new TaskExeLog();
		taskExeLog.setStartTime(Tools.getCurDate(null));
		taskExeLog.setTaskName(this.getTaskName());
		taskExeLog.setType(type().getType());
		String taskLogMemo = null;

		int iBlogCount=0;
		int iBlogComment=0;
		
		// 得到所有日志
		List<Blog> listBlog = rebotBlogService.queryAllRebotWriteBlog();
		for (Blog blog : listBlog) {
			// 得到类型
			TaskType taskType = TaskType.getTaskType(blog.getTask_type());
			// 如果有实现bean
			if (null != taskType.getImpBean()) {
				ICrawlBlogService crawlBlogService = (ICrawlBlogService) BeanFactoryUtil
						.getBean(taskType.getImpBean());
				// 如果评论数量不够
				if (blog.getTotal_comment() < crawlBlogService.getCommentNum()) {
					List<Comment> listCommentOrg = crawlBlogService
							.getComments(blog.getUrl());

					if (null != listCommentOrg && listCommentOrg.size() > 0) {
						// 得到评论
						List<Comment> listComment = new ArrayList<Comment>();
						for (Comment comment : listCommentOrg) {
							Comment c = new Comment();
							String content = replaceCommentContent(comment
									.getComment());
							c.setComment(content);
							listComment.add(c);
						}

						// 得到已经有的评论
						List<Comment> listCommentExsit = rebotBlogService
								.queryListRebotCommentByBid(blog.getBlogId());

						// 比较后剔除已有评论
						listComment = deleteExistComment(listComment,
								listCommentExsit);
						
						//发布新评论
						if(listComment.size()>0)
						{
							//逐个发布评论
							for(Comment c:listComment)
							{
								c.setBid(blog.getId());
								c.setInsert_time(Tools.getCurDate(null));
								c.setStatus(Comment.STATUS_PUBLIC_NO);
								rebotBlogService.insertRebotComment(c);
							}
							//更新日志评论总数
							blog.setTotal_comment(blog.getTotal_comment()+listComment.size());
							rebotBlogService.updateRebotWriteBlog(blog);
							iBlogCount++;
							iBlogComment+=blog.getTotal_comment()+listComment.size();
						}
					}// end if (listComment != null)
				}// end if(blog.getTotal_comment() <
				// crawlBlogService.getCommentNum())
			}// end if (null != taskType.getImpBean())
		}// end for (Blog blog : listBlog)

		taskLogMemo="抓取评论日志数="+iBlogCount+";抓取相应评论数="+iBlogComment;
		taskExeLog.setMemo(taskLogMemo);
		taskExeLog.setEndTime(Tools.getCurDate(null));
		// 记录任务执行日志
		getTaskService().insertTaskExeLog(taskExeLog);
		System.out.println("结束:" + getTaskName());

	}

	private List<Comment> deleteExistComment(List<Comment> newComment,
			List<Comment> existComment) {
		// 复制数组
		List<Comment> listBack = new ArrayList<Comment>();
		for (Comment c : newComment) {
			listBack.add(c);
		}

		Iterator<Comment> itera = newComment.iterator();
		while (itera.hasNext()) {
			Comment comment = itera.next();
			for (Comment ec : existComment) {
				if (ec.getComment().equals(comment.getComment())) {
					// newComment.remove(comment);
					listBack.remove(comment);
					break;
				}
			}
		}
		return listBack;
	}

	private String replaceCommentContent(String comment) {
		String backComment = comment;

		return backComment;
	}

	@Override
	public TaskType type() {
		return TaskType.CrawlComment_Sina_Lady;
	}

	public IRebotBlogService getRebotBlogService() {
		return rebotBlogService;
	}

	public void setRebotBlogService(IRebotBlogService rebotBlogService) {
		this.rebotBlogService = rebotBlogService;
	}

}
